//
//  StatusRetweetedView.swift
//  个人微博
//
//  Created by 一介布衣 on 16/2/24.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    
    // 添加约束对象属性，做记录使用
    var bottomConstraint: Constraint?
    
    var retweeted_status: Status? {
        didSet {
            retweetedLabel.text = retweeted_status?.text
            pictureView.imageURLs = retweeted_status?.imageURLs
            
            // 卸载老的约束对象
            self.bottomConstraint?.uninstall()
            
            // 根据数据调整约束关系
            if let imageURLs = retweeted_status?.imageURLs where imageURLs.count != 0 {
                // 转发微博有配图视图
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                }
            }else {
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(statusCellMargin).constraint
                }
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 设置界面
    private func setupUI() {
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        // 添加约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(statusCellMargin)
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.right.equalTo(self.snp_right).offset(-statusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        // 给转发微博的底部添加约束
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
    }

    
    
    // 懒加载视图
    private lazy var retweetedLabel: UILabel = UILabel(text: "转发微博:终于等到你", fontSize: 14, textColor: UIColor.darkGrayColor(),alignment: .Left)
    
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}











